<template>
  <view class="profile-container">
    <view class="user-card">
      <view class="user-avatar">
        <image :src="userInfo.avatar || '/static/avatar-default.png'" class="avatar" />
      </view>
      <view class="user-info">
        <text class="user-name">{{ userInfo.username }}</text>
        <text class="user-role">学生</text>
        <text class="user-id">学号: {{ userInfo.userId }}</text>
      </view>
    </view>

    <view class="menu-section">
      <view class="section-title">我的功能</view>
      <view class="menu-list">
        <view class="menu-item" @click="navTo('schedule')">
          <view class="menu-icon">📚</view>
          <text class="menu-text">我的课程表</text>
          <text class="menu-arrow">›</text>
        </view>
        
        <view class="menu-item" @click="navTo('classroom')">
          <view class="menu-icon">🏫</view>
          <text class="menu-text">教室预约</text>
          <text class="menu-arrow">›</text>
        </view>
        
        <view class="menu-item" @click="navTo('checkin')">
          <view class="menu-icon">✅</view>
          <text class="menu-text">签到记录</text>
          <text class="menu-arrow">›</text>
        </view>
      </view>
    </view>

    <view class="menu-section">
      <view class="section-title">账户设置</view>
      <view class="menu-list">
        <view class="menu-item" @click="showEditDialog">
          <view class="menu-icon">👤</view>
          <text class="menu-text">修改个人信息</text>
          <text class="menu-arrow">›</text>
        </view>
        
        <view class="menu-item" @click="showPasswordDialog">
          <view class="menu-icon">🔒</view>
          <text class="menu-text">修改密码</text>
          <text class="menu-arrow">›</text>
        </view>
        
        <view class="menu-item" @click="handleLogout">
          <view class="menu-icon">🚪</view>
          <text class="menu-text">退出登录</text>
          <text class="menu-arrow">›</text>
        </view>
      </view>
    </view>

    <!-- 修改信息弹窗 -->
    <uni-popup ref="editPopup" type="center">
      <view class="edit-dialog">
        <text class="dialog-title">修改个人信息</text>
        <view class="form-item">
          <text class="label">姓名</text>
          <input v-model="editForm.username" class="input" />
        </view>
        <view class="form-item">
          <text class="label">头像URL</text>
          <input v-model="editForm.avatar" class="input" />
        </view>
        <view class="dialog-actions">
          <button class="btn cancel" @click="$refs.editPopup.close()">取消</button>
          <button class="btn confirm" @click="saveProfile">保存</button>
        </view>
      </view>
    </uni-popup>

    <!-- 修改密码弹窗 -->
    <uni-popup ref="passwordPopup" type="center">
      <view class="password-dialog">
        <text class="dialog-title">修改密码</text>
        <view class="form-item">
          <text class="label">原密码</text>
          <input v-model="passwordForm.oldPassword" password class="input" />
        </view>
        <view class="form-item">
          <text class="label">新密码</text>
          <input v-model="passwordForm.newPassword" password class="input" />
        </view>
        <view class="form-item">
          <text class="label">确认新密码</text>
          <input v-model="passwordForm.confirmPassword" password class="input" />
        </view>
        <view class="dialog-actions">
          <button class="btn cancel" @click="$refs.passwordPopup.close()">取消</button>
          <button class="btn confirm" @click="changePassword">确认</button>
        </view>
      </view>
    </uni-popup>
  </view>
</template>

<script>
export default {
  data() {
    return {
      userInfo: {},
      editForm: {
        username: '',
        avatar: ''
      },
      passwordForm: {
        oldPassword: '',
        newPassword: '',
        confirmPassword: ''
      }
    }
  },
  
  onLoad() {
    this.loadUserInfo()
  },
  
  methods: {
    loadUserInfo() {
      this.userInfo = uni.getStorageSync('userInfo') || {}
      this.editForm = { ...this.userInfo }
    },
    
    navTo(page) {
      const routes = {
        schedule: '/pages/student/schedule/schedule',
        classroom: '/pages/student/classroom/classroom',
        checkin: '/pages/student/checkin/checkin'
      }
      uni.navigateTo({ url: routes[page] })
    },
    
    showEditDialog() {
      this.$refs.editPopup.open()
    },
    
    showPasswordDialog() {
      this.passwordForm = {
        oldPassword: '',
        newPassword: '',
        confirmPassword: ''
      }
      this.$refs.passwordPopup.open()
    },
    
    saveProfile() {
      // 更新用户信息
      const updatedInfo = { ...this.userInfo, ...this.editForm }
      uni.setStorageSync('userInfo', updatedInfo)
      this.userInfo = updatedInfo
      
      uni.showToast({ title: '修改成功', icon: 'success' })
      this.$refs.editPopup.close()
    },
    
    changePassword() {
      if (!this.passwordForm.oldPassword || !this.passwordForm.newPassword) {
        uni.showToast({ title: '请填写完整信息', icon: 'none' })
        return
      }
      
      if (this.passwordForm.newPassword !== this.passwordForm.confirmPassword) {
        uni.showToast({ title: '两次密码输入不一致', icon: 'none' })
        return
      }
      
      // 这里应该调用修改密码的API
      uni.showToast({ title: '密码修改成功', icon: 'success' })
      this.$refs.passwordPopup.close()
    },
    
    handleLogout() {
      uni.showModal({
        title: '提示',
        content: '确定要退出登录吗？',
        success: (res) => {
          if (res.confirm) {
            // 清除登录信息
            uni.removeStorageSync('token')
            uni.removeStorageSync('userInfo')
            
            // 跳转到登录页
            uni.reLaunch({ url: '/pages/login/login' })
          }
        }
      })
    }
  }
}
</script>

<style scoped>
.profile-container {
  min-height: 100vh;
  background: #f5f5f5;
}

.user-card {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 60rpx 40rpx;
  color: white;
  display: flex;
  align-items: center;
}

.user-avatar {
  margin-right: 30rpx;
}

.avatar {
  width: 120rpx;
  height: 120rpx;
  border-radius: 50%;
  border: 4rpx solid rgba(255, 255, 255, 0.3);
}

.user-info {
  flex: 1;
}

.user-name {
  display: block;
  font-size: 36rpx;
  font-weight: bold;
  margin-bottom: 10rpx;
}

.user-role {
  display: block;
  font-size: 24rpx;
  opacity: 0.9;
  margin-bottom: 5rpx;
}

.user-id {
  font-size: 22rpx;
  opacity: 0.8;
}

.menu-section {
  background: white;
  margin: 20rpx;
  border-radius: 15rpx;
  overflow: hidden;
}

.section-title {
  font-size: 28rpx;
  font-weight: bold;
  color: #333;
  padding: 30rpx;
  border-bottom: 1rpx solid #f0f0f0;
}

.menu-list {
  padding: 0;
}

.menu-item {
  display: flex;
  align-items: center;
  padding: 30rpx;
  border-bottom: 1rpx solid #f0f0f0;
}

.menu-item:last-child {
  border-bottom: none;
}

.menu-icon {
  font-size: 40rpx;
  margin-right: 30rpx;
  width: 60rpx;
  text-align: center;
}

.menu-text {
  flex: 1;
  font-size: 28rpx;
  color: #333;
}

.menu-arrow {
  font-size: 36rpx;
  color: #999;
}

.edit-dialog,
.password-dialog {
  background: white;
  border-radius: 20rpx;
  padding: 40rpx;
  width: 600rpx;
}

.dialog-title {
  display: block;
  font-size: 36rpx;
  font-weight: bold;
  text-align: center;
  margin-bottom: 30rpx;
}

.form-item {
  margin-bottom: 30rpx;
}

.label {
  display: block;
  font-size: 28rpx;
  color: #333;
  margin-bottom: 15rpx;
  font-weight: 500;
}

.input {
  border: 2rpx solid #e0e0e0;
  border-radius: 8rpx;
  padding: 20rpx;
  font-size: 28rpx;
  width: 100%;
  box-sizing: border-box;
}

.dialog-actions {
  display: flex;
  gap: 20rpx;
  margin-top: 40rpx;
}

.btn {
  flex: 1;
  border: none;
  border-radius: 8rpx;
  padding: 20rpx;
  font-size: 28rpx;
}

.cancel {
  background: #f5f5f5;
  color: #666;
}

.confirm {
  background: #667eea;
  color: white;
}
</style>